package com.wonder.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.QingSfcInstock;
import com.wonder.app.domain.QingSfcOperp;
import com.wonder.app.request.QingSfcInstockRequest;
import com.wonder.app.request.QingSfcOperpRequest;
import com.wonder.app.service.QingSfcInstockService;
import com.wonder.app.service.QingSfcOperpService;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.framework.authorization.CheckAdminToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *
 * <p>说明： 销售订单API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Feb 18, 2025
 *
 */
@Api(tags = "生产入库")
@RestController
@RequestMapping("/app/QingSfcInstock")
public class QingSfcInstockController {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private QingSfcInstockService qingSfcInstockService;

    @Autowired
    private CheckAdminToken checkAdminToken;

    @Autowired
    private SysAdminService adminService;

    @ApiOperation(value = "生产订单列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody QingSfcInstockRequest qingSfcInstockRequest) {
        Page<QingSfcInstock> page = new Page<QingSfcInstock>(qingSfcInstockRequest.getPage(), qingSfcInstockRequest.getLimit());
        IPage<QingSfcInstock> list = qingSfcInstockService.selectList(qingSfcInstockRequest, page);
        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data", CommonPage.restPage(list));
        return AjaxMap.success(retMap);
    }



    /**
     * @explain 导入
     * @param   map 对象参数：List<Map>
     * @return  Map<String,Object>
     * @author  Wonder
     */

    @PostMapping("/import_order")
    @ApiOperation(value = "导入", notes = "作者：Wonder")
    public AjaxResult<Object> importData(HttpServletRequest request,@RequestBody List<Map<String, Object>> map ) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        String userName = user.getName();
        Long user_id = user.getUser_id();

        //每个用户一个独立的锁
        String lockKey = "lock:user:" + user_id + ":dowork";
        RLock lock = redissonClient.getLock(lockKey);
        boolean locked = false;
        try {
            locked = lock.tryLock(0,-1, TimeUnit.SECONDS);
            if (!locked){
                throw new RuntimeException("请求处理中，请勿重复请求");
            }
            return qingSfcInstockService.importOrder(userName,user_id,map);
        } catch (Exception e){
            return AjaxResult.failed(e.getMessage());
        } finally {
            // 解锁必须确保是当前线程持有锁
            if (locked && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }

    }





    /**
     * @explain 删除 @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "删除")
    @ResponseBody
    @PostMapping(value = "/delete")

    public AjaxResult<Map<String,Object>> deleteqSfcoperp(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        String userName = user.getName();
        Long user_id = user.getUser_id();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = qingSfcInstockService.deleteQingSfcInstock(userName,user_id,map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }
    /**
     * @explain 查看对象明细
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询saleorder明细")
    @ResponseBody
    @PostMapping(value = "/qingsfcinstock_detail")

    public AjaxResult<Map<String,Object>> getQingSfcInstockDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = qingSfcInstockService.getQingSfcInstockDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }


}
